package com.yiwei.zxp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.BuyListModel;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Spans;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xsooy-pc on 18-6-1.
 */

public class SureOrderExpandAdapter extends BaseExpandableListAdapter {


    private final int x22;
    private final int x28;
    private Context context;
    private List<BuyListModel.DatasBean> datasBeen;

    public SureOrderExpandAdapter(Context context, List<BuyListModel.DatasBean> datasBeen) {
        this.datasBeen = datasBeen;
        this.context = context;
        x22 = context.getResources().getDimensionPixelSize(R.dimen.x22);
        x28 = context.getResources().getDimensionPixelSize(R.dimen.x28);
    }

    @Override
    public int getGroupCount() {
        return datasBeen.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datasBeen.get(groupPosition).items.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datasBeen.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datasBeen.get(groupPosition).items.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_sure_order_group, parent, false);
            holder = new GroupViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        if (datasBeen.get(groupPosition).shop_info.is_self == 1) {
            holder.imgIcon.setImageResource(R.mipmap.img_shop_type_self);
        } else {
            holder.imgIcon.setImageResource(R.mipmap.img_shop_type_other);
        }
        holder.txtName.setText(datasBeen.get(groupPosition).shop_info.shop_name);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_sure_order_child, parent, false);
            holder = new ChildViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        BuyListModel.DatasBean.ItemsBean item = datasBeen.get(groupPosition).items.get(childPosition);
        if (item.photos != null && item.photos.size() != 0) {
            Glide.with(context).load(item.photos.get(0)).asBitmap().centerCrop().into(holder.imgGoods);
        }
        holder.txtTitle.setText(item.title);
        holder.txtMoney.setText("￥" + item.price);
        holder.txtQuickFee.setText("运费:￥" + item.deliver_fee);
        holder.txtNum.setText("x" + item.num);
        if (isLastChild) {
            holder.layoutDikou.setVisibility(View.VISIBLE);

            Spanned txt_dikou = Spans.with(context).font("淘淘币抵扣  ", x28, 0xff010101).
                    font("（可用数量：" + UserInfoGlobal.getTTB() + "个）", x28, 0xff999999).
                    font("\n选择淘淘币抵扣后，如不付款则不退回淘淘币。", x22, 0xff999999).
                    build();
            holder.txtDikou.setText(txt_dikou);
            holder.btnCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    datasBeen.get(groupPosition).is_use_ttb = !datasBeen.get(groupPosition).is_use_ttb;
                    holder.btnCheck.setSelected(datasBeen.get(groupPosition).is_use_ttb);
                    if (onChooseListener != null) {
                        float money = 0;
                        for (int i = 0; i < datasBeen.get(groupPosition).items.size(); i++) {
                            money += Float.valueOf(datasBeen.get(groupPosition).items.get(i).price) * datasBeen.get(groupPosition).items.get(i).num;
//                            money += Float.valueOf(datasBeen.get(groupPosition).items.get(i).deliver_fee);
                        }
//                        Log.i("dasdassdasadada", "数据：" + datasBeen.get(groupPosition).is_use_ttb + "钱=" + money);
                        onChooseListener.onClick(groupPosition, datasBeen.get(groupPosition).is_use_ttb, money);
                    }
                }
            });
        } else {
            holder.layoutDikou.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    static class GroupViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_name)
        TextView txtName;

        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildViewHolder {
        @BindView(R.id.img_goods)
        ImageView imgGoods;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_quick_fee)
        TextView txtQuickFee;
        @BindView(R.id.txt_num)
        TextView txtNum;
        @BindView(R.id.txt_dikou)
        TextView txtDikou;
        @BindView(R.id.btn_check)
        ImageView btnCheck;
        @BindView(R.id.layout_dikou)
        FrameLayout layoutDikou;

        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(int poi, boolean is_use, float money);
    }

}
